package com.yumei.trader.gym.page;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yumei.trader.gym.R;
import com.yumei.trader.gym.act.BSAction;
import com.yumei.trader.gym.client.BasePage;
import com.yumei.trader.gym.page.Adapter.GameRecordAdapter;
import com.yumei.trader.gym.util.StringUtils;

import java.util.ArrayList;

public class PagePpGameResult extends BasePage {

    private String stockCode,stockName;
    private String date;
    private float profitRate;
    private float profit;
    private ArrayList<BSAction> record;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        stockCode = this.getIntent().getStringExtra("stockCode");
        stockName = this.getIntent().getStringExtra("stockName");
        date = this.getIntent().getStringExtra("date");
        profit = this.getIntent().getFloatExtra("profit", 0);
        profitRate = this.getIntent().getFloatExtra("profitRate", 0);
        record = (ArrayList<BSAction>) this.getIntent().getSerializableExtra("record");

        setContentView(R.layout.pop_game_result);

        registerComponent();
        transparentBar();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //界面全屏，隐藏导航栏
        hideBottomNav(PagePpGameResult.this);
    }

    @SuppressLint({"SetTextI18n", "DefaultLocale"})
    private void registerComponent() {

        TextView stockCode = this.findViewById(R.id.game_record_stock_code);
        stockCode.setText(this.stockCode.substring(3));
        TextView stockName = this.findViewById(R.id.game_record_stock_name);
        stockName.setText(this.stockName);

        TextView stockDate = this.findViewById(R.id.game_record_stock_date);
        stockDate.setText(date);
        TextView profitRate = this.findViewById(R.id.game_record_profit_rate);

        TextView profitValue = this.findViewById(R.id.game_record_profit_value);

        if (profit > 0){
            profitRate.setText("+" + String.format("%.2f", this.profitRate) + "%");
            profitValue.setText("+" + StringUtils.moneyFormat((int)profit, 2));
            profitRate.setTextColor(ContextCompat.getColor(this, R.color.color_stock_down));
            profitValue.setTextColor(ContextCompat.getColor(this, R.color.color_stock_down));
        }else if (profit < 0){
            profitRate.setText("" + String.format("%.2f", this.profitRate) + "%");
            profitValue.setText("" + StringUtils.moneyFormat((int)profit, 2));
            profitRate.setTextColor(ContextCompat.getColor(this, R.color.color_stock_up));
            profitValue.setTextColor(ContextCompat.getColor(this, R.color.color_stock_up));
        }else {
            profitRate.setText("" + String.format("%.2f", this.profitRate) + "%");
            profitValue.setText("" + StringUtils.moneyFormat((int)profit, 2));
            profitRate.setTextColor(ContextCompat.getColor(this, R.color.color_font_main));
            profitValue.setTextColor(ContextCompat.getColor(this, R.color.color_font_main));
        }

        ListView recordList = this.findViewById(R.id.list_game_record);
        GameRecordAdapter recordAdapter = new GameRecordAdapter(this);
        recordAdapter.addItems(record);
        recordList.setAdapter(recordAdapter);

        this.findViewById(R.id.game_result_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackAction(0x200);
            }
        });

        this.findViewById(R.id.game_result_review).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackAction(0x201);
            }
        });

        this.findViewById(R.id.game_result_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackAction(0x202);
            }
        });
    }
}
